package com.wonyesoft.wuliu.acticity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionResult.SuggestionInfo;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.acticity.order.AddCargoOrderActivity;

public class AddCargoMapActivity extends BaseActivity implements
		OnClickListener {

	private TextView title, head_right, cargoinfo_cargotypetxt,
			cargoinfo_cargovolumetxt, cargoinfo_cargoweighttxt,
			cargoinfo_cargocartypetxt;
	private ImageView addcargo_cancle;
	private EditText driver_carnumber;
	private ListView addcargo_listmap;
	private SimpleAdapter adapter;
	// private SuggestionSearch mSuggestionSearch;
	private String seachWords;
	private MapView mMapView = null;
	private BaiduMap mBaiduMap = null;
	private BitmapDescriptor bitmap;
	private AppContent app;
	private SuggestionSearch mSuggestionSearch;
	private List<Map<String, Object>> list;
	private HashMap<String, String> mapm = null;
	private int resultCode;

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SDKInitializer.initialize(AppContent.getContext());
		app = (AppContent) getApplication();
		setContentView(R.layout.aj_addcargo_selectmap);
		mSuggestionSearch = SuggestionSearch.newInstance();
		findIds();
		blindOnclick();
		addBaiduMapPoint();
		OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
			public void onGetSuggestionResult(SuggestionResult res) {
				if (res == null || res.getAllSuggestions() == null) {

					return;
					// 未找到相关结果
				} else {

					initListAdrr(res.getAllSuggestions());

				}
				// 获取在线建议检索结果
			}
		};
		mSuggestionSearch.setOnGetSuggestionResultListener(listener);

	}

	private List<Map<String, Object>> getData(
			List<SuggestionInfo> allSuggestions) {
		System.out.println("-----START-----");
		for (SuggestionInfo c : allSuggestions) {
			HashMap<String, Object> map = new HashMap<String, Object>();;
			System.out.println("ket:" + c.key);
			map.put("a0_common", c.key);
			map.put("a1_common", c.city + c.district + c.key);

			list.add(map);
		}
		System.out.println("-----END-----");
		
		return list;
	}

	Intent tochangeIntent;
	Bundle bundle;

	private void initListAdrr(List<SuggestionInfo> allSuggestions) {
		list = new ArrayList<Map<String, Object>>();
		adapter = new SimpleAdapter(AddCargoMapActivity.this, getData(allSuggestions),
				R.layout.activity_cargoaddr, new String[] { "a0_common",
						"a1_common" }, new int[] { R.id.a0_common,
						R.id.a1_common });
		addcargo_listmap.setAdapter(adapter);
		addcargo_listmap.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mapm = (HashMap<String, String>) addcargo_listmap
						.getItemAtPosition(position);

//				tochangeIntent = new Intent(AddCargoMapActivity.this,
//						ChangeCargoActivity.class);
				tochangeIntent = new Intent(AddCargoMapActivity.this,
				AddCargoOrderActivity.class);
				tochangeIntent.putExtra("address", mapm.get("a1_common"));
				bundle = new Bundle();
				bundle.putString("addr", mapm.get("a1_common"));
				System.out.println("RESULT_OK" + RESULT_OK);
				setResult(RESULT_OK, tochangeIntent);
				finish();
			}
		});
	}

	private void addBaiduMapPoint() {
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
		// mBaiduMap.setTrafficEnabled(true);
		// mBaiduMap.setBaiduHeatMapEnabled(true);
		//System.out.println(app.adrr);
		//System.out.println(app.latitude);
		//System.out.println(app.longitude);
		// 定义Maker坐标点
		LatLng point = new LatLng(app.latitude, app.longitude);
		LatLng point1 = new LatLng(app.latitude + 1, app.longitude);
		LatLng point2 = new LatLng(app.latitude - 1, app.longitude);
		// 构建Marker图标
		bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka);
		MyLocationConfiguration config = new MyLocationConfiguration(null,
				true, BitmapDescriptorFactory.fromResource(R.drawable.icon_geo));
		mBaiduMap.setMyLocationConfigeration(config);
		// 构建MarkerOption，用于在地图上添加Marker
		OverlayOptions option = new MarkerOptions().position(point)
				.icon(bitmap);
		// 在地图上添加Marker，并显示
		mBaiduMap.addOverlay(option);

		MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(point);
		mBaiduMap.animateMapStatus(u);

		mBaiduMap.setMyLocationEnabled(false);

	}

	protected void onDestroy() {
		super.onDestroy();
		// 在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
		mMapView.onDestroy();
	}

	protected void onResume() {
		super.onResume();
		// 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
		mMapView.onResume();
	}

	protected void onPause() {
		super.onPause();
		// 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
		mMapView.onPause();
	}

	private void blindOnclick() {
		// addcargo_cancle.setVisibility(0);
		addcargo_cancle.setOnClickListener(this);
		// driver_carnumber.setOnClickListener(this);
		driver_carnumber.addTextChangedListener(new TextWatcher() {

			@SuppressLint("NewApi") @Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {

				seachWords = arg0 + "";
				if (null != seachWords || seachWords.isEmpty()) {
					System.out.println(seachWords);
					mSuggestionSearch
							.requestSuggestion((new SuggestionSearchOption())
									.keyword(seachWords).city("重庆市"));
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				// System.out.println(arg0);
			}
		});

	}

	protected void getmap(String seachWords2) {

	}

	private void findIds() {
		driver_carnumber = (EditText) findViewById(R.id.driver_carnumber);
		addcargo_cancle = (ImageView) findViewById(R.id.addcargo_cancle);
		addcargo_listmap = (ListView) findViewById(R.id.addcargo_listmap);
		addcargo_listmap.setDividerHeight(0);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.addcargo_cancle:
			finish();
			break;
		case R.id.driver_baochun:
			break;
		default:
			break;
		}
	}

	/*
	 * private void getmap( String seachWords) { System.out.println("进来了啊");
	 * mSuggestionSearch = SuggestionSearch.newInstance();
	 * 
	 * OnGetSuggestionResultListener listener = new
	 * OnGetSuggestionResultListener() { public void
	 * onGetSuggestionResult(SuggestionResult res) {
	 * System.out.println("onget进来了啊"); if (res == null ||
	 * res.getAllSuggestions() == null) { System.out.println("没结果"); return;
	 * //未找到相关结果 } //获取在线建议检索结果 System.out.println("有结果");
	 * res.getAllSuggestions(); for(SuggestionInfo c :res.getAllSuggestions() ){
	 * 
	 * System.out.println(c.district); } } }; //第三步，设置在线建议查询监听者；
	 * mSuggestionSearch.setOnGetSuggestionResultListener(listener);
	 * //第四步，发起在线建议查询； // 使用建议搜索服务获取建议列表，结果在onSuggestionResult()中更新
	 * mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
	 * .keyword(seachWords) .city("重庆")); //第五步，释放在线建议查询实例；
	 * //mSuggestionSearch.destroy();
	 * 
	 * }
	 */

}
